class Advertisment < ActiveRecord::Base
  belongs_to :sub_category
  belongs_to :lead
  belongs_to :university
  has_many :queries
  has_many :attachments
  has_many :comments
  has_many :relevance_factors

  belongs_to :user

  has_and_belongs_to_many :user_types
  has_and_belongs_to_many :tags
  has_one :advertisment_attribute_field_value

  has_many :relevant_advertisments, :through => :relevance_factors
  auto_tags :include => [:description]
  validates_uniqueness_of :user_id, :scope => :title, :scope => :description

  acts_as_sphinx

  #  This is a function to create tag at once, after tags population, it should be called after_filter of advertisments
  def generate_and_add_tags_to_system_from_description
    unless self.description.empty?
      tags_suggested = self.get_auto_tags
      puts "Tags generated count: #{tags_suggested.length}"
      tags_suggested_filtered = tags_suggested - [", "]
      tags_suggested_filtered.each do |tag_to_use|
        tag = Tag.find_or_create_by_name(tag_to_use)
        self.tags << tag
        puts "------ '#{tag.name}' added."
      end
    else
      puts "No description found!!"
    end
  end
end
